/*
* $Id: WizardForm.java 250 2011-01-17 21:36:24Z volok-aleksej@yandex.ru $
*
* Copyright 2008-2011 by Volokitin Alexey
* All rights reserved
*
*/
package apiary.ui.journal;

import apiary.Apiary;
import apiary.component.journal.Wizard;
import apiary.ui.BackScreen;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;

public abstract class WizardForm extends BackScreen implements CommandListener,
                                                               WizardScreen
{
    protected Integer idForm;
    protected Wizard wizard;
    
    protected Command okCommand;
    protected Command backCommand;
    protected Command cancelCommand;

    public WizardForm(String name, Wizard wiz, Integer id, boolean nextCommand) {
        super(name);
        idForm = id;
        wizard = wiz;

        setCommandListener(this);
        Apiary apiary = Apiary.getInstance();
        if(nextCommand)
            okCommand = new Command(apiary.getLocaleString("Next"), Command.SCREEN, 1);
        else
            okCommand = new Command(apiary.getLocaleString("Save"), Command.SCREEN, 1);
        addCommand(okCommand);
        backCommand = new Command(apiary.getLocaleString("Back"), Command.SCREEN, 2);
        addCommand(backCommand);
        cancelCommand = new Command(apiary.getLocaleString("Cancel"), Command.SCREEN, 3);
        addCommand(cancelCommand);
    }

    public Integer getIdForm() {
        return idForm;
    }

    public void commandAction(Command c, Displayable d) {
        Apiary apiary = Apiary.getInstance();
        if(c.getPriority() == 1) {
            commandOk();
        } else if(c.getPriority() == 2) {
            apiary.Display(backForm);
        } else if(c.getPriority() == 3) {
            apiary.Display(wizard.getCancelForm());
        } else {
            commandUser(c);
        }
    }

    protected void commandOk(){
        BackScreen nextForm = wizard.getNextForm(idForm);
        Object dataForm = GetDataForm();
        if(!wizard.isOk(idForm, dataForm))
            return;
        wizard.setData(idForm, dataForm);
        if(nextForm == null) {
            wizard.save();
            nextForm = wizard.getCancelForm();
        } else {
            wizard.next(nextForm);
        }
        Apiary.getInstance().Display(nextForm);
    }

    protected abstract void commandUser(Command command);
}
